package com.wskj.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.wskj.system.bo.SysVipConfigAddBo;
import com.wskj.system.bo.SysVipConfigEditBo;
import com.wskj.system.bo.SysVipConfigQueryBo;
import com.wskj.system.service.ISysVipConfigService;
import com.wskj.system.vo.web.SysVipConfigVo;
import lombok.AllArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.annotation.Log;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 会员卡配置Controller
 *
 * @author wsdz
 * @date 2023-03-06
 */
@Api(value = "会员卡配置控制器", tags = {"会员卡配置管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/system/vip/config")
public class SysVipConfigController extends BaseController {

    private final ISysVipConfigService iSysVipConfigService;

    /**
     * 查询会员卡配置列表
     */
    @ApiOperation("查询会员卡配置列表")
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo<SysVipConfigVo> list(@Validated SysVipConfigQueryBo bo) {
        return iSysVipConfigService.queryPageList(bo);
    }

    /**
     * 导出会员卡配置列表
     */
    @ApiOperation("导出会员卡配置列表")
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @Log(title = "会员卡配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated SysVipConfigQueryBo bo) {
        List<SysVipConfigVo> list = iSysVipConfigService.queryList(bo);
        ExcelUtil<SysVipConfigVo> util = new ExcelUtil<>(SysVipConfigVo.class);
        return util.exportExcel(list, "会员卡配置");
    }

    /**
     * 获取会员卡配置详细信息
     */
    @ApiOperation("获取会员卡配置详细信息")
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping("/{id}")
    public AjaxResult<SysVipConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iSysVipConfigService.queryById(id));
    }

    /**
     * 新增会员卡配置
     */
    @ApiOperation("新增会员卡配置")
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "会员卡配置", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysVipConfigAddBo bo) {
        return toAjax(iSysVipConfigService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改会员卡配置
     */
    @ApiOperation("修改会员卡配置")
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "会员卡配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysVipConfigEditBo bo) {
        return toAjax(iSysVipConfigService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除会员卡配置
     */
    @ApiOperation("删除会员卡配置")
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "会员卡配置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iSysVipConfigService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入会员卡配置数据
     */
    @ApiOperation("导入会员卡配置数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('system:config:import')")
    @Log(title = "会员卡配置" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<SysVipConfigVo> util = new ExcelUtil<>(SysVipConfigVo.class);
        List<SysVipConfigVo> sysVipConfigList;
        try {
            sysVipConfigList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iSysVipConfigService.importSysVipConfig(sysVipConfigList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<SysVipConfigVo> util = new ExcelUtil<>(SysVipConfigVo.class);
        return util.importTemplateExcel("会员卡配置数据");
    }
}
